import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store'

const routes = [
  /************************根路径跳转************************/
  {
	  path: '/',
	  redirect: '/home/index',
  },
  /**************************PC开放页面***********************/
  {
    path: '/home/index',
	component: ()=>import('../views/home/index/index/index'),
	meta:{
		title:"首页",
		requireAuth:false,
		identity:0
	}
  },
  {
    path: '/home/weixin',
  	component: ()=>import('../views/home/index/weixin/index'),
  	meta:{
  		title:"关注官方微信公众号",
  		requireAuth:false,
  		identity:0
  	}
  },
  /*********************************微信端开放页**********************************/
  {
    path: '/wechat/studentauthlogin',//微信授权登录页面
    component: ()=>import('../views/wechat/index/studentauthlogin/index'),
  	meta:{
  		title:"学生授权登录",
  		requireAuth:false,
		identity:0
  	}
  },
  {
    path: '/wechat/lawyerauthlogin',//微信授权登录页面
    component: ()=>import('../views/wechat/index/lawyerauthlogin/index'),
  	meta:{
  		title:"律师授权登录",
  		requireAuth:false,
  		identity:0
  	}
  },
  {
    path: '/wechat/studentlogin',//学生登录页
    component: ()=>import('../views/wechat/index/studentlogin/index'),
  	meta:{
  		title:"学生登录",
  		requireAuth:false,
  		identity:0
  	}
  },
  {
    path: '/wechat/leaderLogin',//领导登录页面
    name: 'LeaderLogin',
    component: ()=>import('../views/wechat/index/LeaderLogin.vue'),
	meta:{
		title:"领导登录",
		requireAuth:false,
		identity:0
	}
  },
  {
    path: '/wechat/teacherlogin',//教师登录页面
    component: ()=>import('../views/wechat/index/teacherlogin/index'),
  	meta:{
  		title:"教师登录",
  		requireAuth:false,
		identity:0
  	}
  },
  {
    path: '/wechat/lawyerlogin',//律师登录页面
    component: ()=>import('../views/wechat/index/lawyerlogin/index'),
  	meta:{
  		title:"律师登录",
  		requireAuth:false,
  		identity:0
  	}
  },
  {
    path: '/wechat/privacypolicy',
    component: ()=>import('../views/wechat/index/privacypolicy/index'),
  	meta:{
  		title:"隐私政策",
  		requireAuth:false,
  		identity:0
  	}
  },
  {
    path: '/wechat/useragreement',
    component: ()=>import('../views/wechat/index/useragreement/index'),
  	meta:{
  		title:"用户协议",
  		requireAuth:false,
  		identity:0
  	}
  },
  /******************************微信端学生可访问页面*********************************/
  {
    path: '/wechatstudent/studentcenter',//学生中心页面
    component: ()=>import('../views/wechat/student/studentcenter/index'),
  	meta:{
  		title:"学生中心",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/setup',//设置
    component: ()=>import('../views/wechat/student/setup/index'),
  	meta:{
  		title:"设置",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/security',//安全中心
    component: ()=>import('../views/wechat/student/security/index'),
  	meta:{
  		title:"安全中心",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/authorizeinfo',//学生授权页面
    component: ()=>import('../views/wechat/student/authorizeinfo/index'),
  	meta:{
  		title:"授权中心",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/lawyerlist',//律师列表页面
    component: ()=>import('../views/wechat/student/lawyerlist/index'),
  	meta:{
  		title:"律师列表",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/consultadd',//咨询新增页面
    component: ()=>import('../views/wechat/student/consultadd/index'),
  	meta:{
  		title:"咨询新增",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/studentinfo',//用户资料页面
    component: ()=>import('../views/wechat/student/studentinfo/index'),
  	meta:{
  		title:"用户资料",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/consultlist',//咨询记录列表页面
    component: ()=>import('../views/wechat/student/consultlist/index'),
  	meta:{
  		title:"咨询记录",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/chatroom',//聊天室
    component: ()=>import('../views/wechat/student/chatroom/index'),
  	meta:{
  		title:"聊天室",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/consumerecord',
    component: ()=>import('../views/wechat/student/consumerecord/index'),
  	meta:{
  		title:"消费记录",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/legalright',
    component: ()=>import('../views/wechat/student/legalright/index'),
  	meta:{
  		title:"当前权益",
  		requireAuth:true,
  		identity:1
  	}
  },
  {
    path: '/wechatstudent/ad',
    component: ()=>import('../views/wechat/student/ad/index'),
  	meta:{
  		title:"“弘法扬宪，律满重财”主题法律知识竞赛",
  		requireAuth:true,
  		identity:1
  	}
  },
  /*******************************微信端教师可访问页******************************/
  {
    path: '/wechatteacher/teachercenter',//教师中心页面
    component: ()=>import('../views/wechat/teacher/teachercenter/index'),
  	meta:{
  		title:"教师中心",
  		requireAuth:true,
  		identity:2
  	}
  },
  {
    path: '/wechatTeacher/authorizeInfo',//微信授权信息页面
    //name: 'AuthorizeInfo',
    component: ()=>import('../views/wechat/teacher/AuthorizeInfo.vue'),
  	meta:{
  		title:"微信授权信息",
  		requireAuth:true,
  		identity:2
  	}
  },
  /*******************************微信端领导可访问页******************************/
  {
    path: '/wechatLeader/leaderCenter',//领导中心页面
    name: 'LeaderCenter',
    component: ()=>import('../views/wechat/leader/LeaderCenter.vue'),
	meta:{
		title:"领导中心",
		requireAuth:true,
		identity:3
	}
  },
  {
    path: '/wechatleader/riskwarning',//风险预警页面
    name: 'RiskWarning',
    component: ()=>import('../views/wechat/leader/RiskWarning.vue'),
  	meta:{
  		title:"风险预警",
  		requireAuth:false,
		identity:0
  	}
  },
  {
    path: '/wechatLeader/reportList',//消息列表页面
    //name: 'ReportList',
    component: ()=>import('../views/wechat/leader/ReportList.vue'),
  	meta:{
  		title:"领导专线消息列表",
  		requireAuth:true,
		identity:3
  	}
  },
  {
    path: '/wechatLeader/authorizeInfo',//微信授权信息页面
    //name: 'AuthorizeInfo',
    component: ()=>import('../views/wechat/leader/AuthorizeInfo.vue'),
  	meta:{
  		title:"微信授权信息",
  		requireAuth:true,
  		identity:3
  	}
  },
  /******************************微信端律师可访问页面*********************************/
  {
    path: '/wechatlawyer/lawyercenter',//律师中心页面
    component: ()=>import('../views/wechat/lawyer/lawyercenter/index'),
  	meta:{
  		title:"律师中心",
  		requireAuth:true,
  		identity:6
  	}
  },
  {
    path: '/wechatlawyer/authorizeinfo',//微信授权信息页面
    component: ()=>import('../views/wechat/lawyer/authorizeinfo/index'),
  	meta:{
  		title:"微信授权信息",
  		requireAuth:true,
  		identity:6
  	}
  },
  {
    path: '/wechatlawyer/consultlist',//咨询工单列表
    component: ()=>import('../views/wechat/lawyer/consultlist/index'),
  	meta:{
  		title:"咨询工单列表",
  		requireAuth:true,
  		identity:6
  	}
  },
  {
    path: '/wechatlawyer/consultdetail',//咨询工单详情
    component: ()=>import('../views/wechat/lawyer/consultdetail/index'),
  	meta:{
  		title:"咨询工单详情",
  		requireAuth:true,
  		identity:6
  	}
  },
  {
    path: '/wechatlawyer/riskwarningadd',//风险预警新增
    component: ()=>import('../views/wechat/lawyer/riskwarningadd/index'),
  	meta:{
  		title:"风险预警新增",
  		requireAuth:true,
  		identity:6
  	}
  },
  {
    path: '/wechatlawyer/chatroom',//聊天室
    component: ()=>import('../views/wechat/lawyer/chatroom/index'),
  	meta:{
  		title:"聊天室",
  		requireAuth:true,
  		identity:6
  	}
  },
  {
    path: '/wechatLawyer/reportList',//领导专线消息列表
    //name: 'ReportList',
    component: ()=>import('../views/wechat/lawyer/ReportList.vue'),
  	meta:{
  		title:"领导专线消息列表",
  		requireAuth:true,
  		identity:6
  	}
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

/**
 * to:即将要进入的目标对象
 * from:当前导航即将要离开的路由对象
 * next:调用该方法,才能进入下一个钩子,
 * afterEach不需要调用next方法
 */
router.beforeEach((to, from, next) => {
	//从vuex里面准备好身份和token令牌
	var token = store.state.globalData.token;
	var identity = store.state.globalData.identity;
	var expireTime = store.state.globalData.expireTime;
	var nowTime = Date.parse(new Date()) / 1000;
	/* 路由发生变化修改页面title */
	if (to.meta.title) {
		document.title = to.meta.title
	}
	if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
		if (!token) { // 判断本地是否存在token
			// 未登录,跳转到登陆页面
			next({
				path: '/'
			})
			return 0;
		}
		if (nowTime >= expireTime) {
			// 身份信息已经过期,跳转到登陆页面
			next({
				path: '/'
			})
			return 0;
		}
		if (to.meta.identity != 0 && (!identity || identity != to.meta.identity)) {
			// 身份有问题
			next({
				path: '/'
			})
			return 0;
		}
	}
	next();
})

export default router

